import 'dart:convert';
import 'mock_interceptor.dart';

class HttpClient {
  // ******* 切换开关 *******
  static const bool _isMock = true; // true=用假数据，false=走真接口

  // 真实接口基地址（现在用不到，留着以后换）
  static const String _baseUrl = 'https://your.api.com';

  /// GET 请求封装
  static Future<List<T>> get<T>(
    String endpoint,
    T Function(Map<String, dynamic>) fromJson,
  ) async {
    final url = _isMock ? endpoint : '$_baseUrl$endpoint';
    try {
      final raw = _isMock ? await mockResponse(url) : await _realGet(url);
      final list = (jsonDecode(raw) as List).cast<Map<String, dynamic>>();
      return list.map((e) => fromJson(e)).toList();
    } catch (e) {
      throw Exception('请求失败：$e');
    }
  }

  ///  POST 请求封装（登录用）
  static Future<Map<String, dynamic>> post(
    String endpoint,
    Map<String, dynamic> body,
  ) async {
    final url = _isMock ? endpoint : '$_baseUrl$endpoint';
    final raw = _isMock ? await mockResponse(url) : await _realPost(url, body);
    return jsonDecode(raw);
  }

  /* ======================
   * 下面先留空，后续接真接口时实现
   * ====================== */
  static Future<String> _realGet(String url) async {
    throw UnimplementedError('真 GET 未实现');
  }

  static Future<String> _realPost(String url, Map<String, dynamic> body) async {
    throw UnimplementedError('真 POST 未实现');
  }
}
